#include "task/commnunication/localhost.h"

using namespace task;

bool LocalHost::Init(std::string param_file)
{
    LoadConfigParam(param_file);
    return true;
}

void LocalHost::LoadConfigParam(std::string param_file)
{
    task_id_ = 0;
    task_id_cnt_ = 1;

    YAML::Node vehicle_params = YAML::LoadFile(param_file);
    YAML::Node task_params = vehicle_params["simulation"];

    task_file_path_ = task_params["task_file_path"].as<std::string>();
    task_num_       = task_params["task_num"].as<int>();
    printf("LoadConfigParam() --> file: %s, num: %d\n", task_file_path_.c_str(), task_num_);
}

void LocalHost::Reset()
{
    SN_line_ = 0;
}

void LocalHost::ReadData(sTaskReceive &task_cmd)
{
    task_cmd.point_list.clear();
    task_cmd.header = "";
    char buff[5000] = {0};
    char x;
    int total = 0;
    task_id_++;
    if (task_id_ > task_num_)
    {
        task_id_ = 1;
    }
    std::string tmp = task_file_path_ + "/TEST" + std::to_string(task_id_) + ".json";
    printf("tmp: %s\n", tmp.c_str());

    Json::Reader reader;
    Json::Value root;
    std::ifstream in(task_file_path_ + "/TEST" + std::to_string(task_id_) + ".json", std::ios::binary);
    if (!reader.parse(in, root))
    {
        std::cerr << "Open File Fail." << std::endl;
        return ;
    }
    // TODO for Insert Error State
    task_cmd.header = root["headers"].as<std::string>();
    task_cmd.SN     = root["SN"].as<int>();
    task_cmd.task_id = task_id_cnt_++;//root["task_id"].as<int>();
    task_cmd.map_id = root["map_id"].as<std::string>();
    task_cmd.command = root["command"].as<std::string>();
    task_cmd.target_id = root["target_id"].as<std::string>();
    task_cmd.speed_level = root["speed_level"].as<float>();

    task_cmd.work_task.clear();
    task_cmd.point_list.clear();

    if (!root["headers"].isNull() && !root["command"].isNull() && !root["points"].isNull())
    {
        if (task_cmd.header == "task" && task_cmd.command == "working")
        {
            if (root["points"].size() == 1)
            {
                std::string action = root["points"][0]["action"].as<std::string>();
                std::string param  = root["points"][0]["param"].as<std::string>();
                task::sWorkTask work_task;
                ParseWorkTask(work_task, action, param);
                printf("value: %f, id: %d\n", work_task.value, work_task.work_id);
                task_cmd.work_task.push_back(work_task);
                task_cmd.point_list.push_back(root["points"][0]["point"].as<std::string>());
            }
            else
            {
                for (int i=0;i<root["points"].size();i++)
                {
                    task_cmd.point_list.push_back(root["points"][i]["point"].as<std::string>());
                }                
            }
        }
    }

    printf("header: %s, SN: %s, size: %ld %d\n", task_cmd.header.c_str(), task_cmd.command.c_str(), task_cmd.point_list.size(), root["points"].size());

    // for (auto p : task_cmd.point_list)
    // {
    //     printf("point: %s\n", p.c_str());
    // }
}

void LocalHost::WriteData(std::string write_mode, sAMRState &amr_state, unsigned int _SN)
{

}

void LocalHost::WriteData(std::string write_mode, sAMRState &amr_state)
{

}

void LocalHost::WriteData(std::string write_mode, std::string file_path)
{

}